<?php


namespace Modules\User\Http\Controllers\Web;


use App\Http\Controllers\MyController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Modules\User\Models\User;
use App\Models\UserInfo;
use Illuminate\Http\Request;
use Modules\User\Models\UserPoint;
use Addons\School\Models\School;
use Modules\System\Models\Region;
use Modules\Cms\Models\ArticleMeta;
use Modules\User\Http\Requests\UserInfoRequest;
class UserInfoController extends MyController
{
    public function info()
    {
        $referrer = request()->headers->get('referer');
 
        session([
            'the_page' => 'info',
            'page_title' => "完善资料",
        ]);
        $mobile= auth()->user()->mobile;
        $uid= auth()->user()->id;
        $list=School::orderBy('id','desc')->get();
        $region=Region::where('level',1)->orderBy('id','asc')->get();
        $userInfo=UserInfo::where('user_id',$uid)->first();
        //$mobile=User::where('id',$id)->pack('mobile');
        return $this->theme('info',['info'=>$userInfo,'list'=>$list,'region'=>$region,'mobile'=>$mobile,'referrer'=>$referrer]);
    }
    public function save(UserInfoRequest $request,UserInfo $userInfo)
    {
        $data = $request->validated();
        $uid= auth()->user()->id;
        $info = UserInfo::where('user_id',$uid)->first();
        if ($info) {
            $info->name=$data['name'];
            $info->school_name=$data['school_name'];
            $info->class_name=$data['class_name'];
            $info->tel=$data['tel'];
            $info->sex=$data['sex'];
            $info->pname=$data['pname'];
            $result=$info->save();
             User::where('id',$uid)->update(['nickname'=>$data['name']]);
            return $this->result($result, ['msg' => '修改成功']);
        }else {
            $data['user_id']=$uid;
            $result = $userInfo->store($data);
             User::where('id',$uid)->update(['nickname'=>$data['name']]);
            return $this->result($result, ['msg' => '修改成功']);
        }
       return $this->result(false, ['msg' => "修改失败"]);
    }
    public function signin(Request $request)
    {
        session([
            'the_page' => 'signin',
            'page_title' => "签到学习",
        ]);
        $aid=$request->get('id');
        $uid= auth()->user()->id;
        $point=ArticleMeta::where('article_id',$aid)->where('meta_key','sign_point')->value('meta_value')?:0;
        $day=UserPoint::where('user_id',$uid)->where('aid',$aid)->where('type',1)->whereDate('created_at', date('Y-m-d'))->orderBy('id','desc')->first();
       if(!$day&&$point) {
            $pointRes = app('user')->point($point, $uid, $aid,1,'签到+'.$point);
        }
        return $this->theme('signin',['point'=>$point]);
    }
    
    
    
    
}